<?php
/**
 * @author      :   QuangLT
 * @name        :   Fpt_Plugin_Block
 * @copyright   :   Fpt Online
 * @todo        :   Envirement plugin block
 */
class Fpt_Plugin_Block
{    	
	/**
	 * Zend_Layout instance
	 * @var Zend_Layout
	 */
	private static $_layout = null;
    
    /**
     * Array default return
     * @var <array>
     */
    private static $_arrReturn = array(
                                        'name'      =>  'layout',
                                        'folder'    =>  'default',
                                        'sections'  =>  array(),
                                        'metadatas' =>  array()
                                    );
    
    /**
     * Constructor
     *
     */
	private final function __construct() {}
    
    /**
     * Check layout instance
     */
	private static function checkLayout()
	{
        //Check to layout instance
		if(is_null(self::$_layout))
		{
			//Set Layout Instance
            self::$_layout = Zend_Layout::getMvcInstance();
            
            //Set Sections Helper
            if(empty(self::$_layout->_sections))
            {                
                self::$_layout->_sections = array();
            }

            //Set Metadata Helper
            if(empty(self::$_layout->_metadatas))
            {
                self::$_layout->_metadatas = array();
            }
		}        
	}
    
    /**
	 * Get layout
     * @return <array>
	 */
	public static function getLayouts()
	{	        
		//Return Layout
		return self::$_arrReturn['layout'];
	}
    
    /**
     * Set section for layout
     * @param <string> $section
     * @param <string> $content
     */
    private static function setSection($section, $content)
    {
        //Set section with content
		$arrSections = self::$_layout->_sections;
		$arrSections[$section] = $content;
		self::$_layout->_sections = $arrSections;
        
		//Cleanup
		unset($arrSections);
    }
    
    /**
     * get layout section data from DB
     */
    private static function getLayoutSection($cateID, $module, $controller, $action)
    {
        //Get block model
        $blockModel = layout::getInstance();
        
        $mapParams = array(
                'Module'        => $module,
                'Controller'    => $controller,
                'Action'        => $action,
                'CateID'        => $cateID,
            );
        //Get layout data
        $arrReturn  = $blockModel->getLayoutFull($mapParams);
        
        //var_dump('<pre>',$cateID, $module, $controller, $action, $arrReturn); exit;
        
        //Return data
        return $arrReturn;
    }
    
    /**
     * Process Block MVC
     * Get data block and display layout
     */
    public static function processBlockMvc($cateID, $module, $controller, $action)
    {
        //Get data layout, section
        $arrReturn  = self::getLayoutSection($cateID, $module, $controller, $action);

        //Set layout data
        self::$_arrReturn = $arrReturn;
    }
    
    /**
     * Process sections in layouts 
     */
    public static function processSections()
    {
        //Check to layout instance
        self::checkLayout();
        
        //Check sections
        if(empty(self::$_arrReturn['sections']))
        {
            self::$_arrReturn['sections'] = array();
            return;
        }
        
        //Loop for render sections
        foreach(self::$_arrReturn['sections'] as $sectionName => $block)
        {
            //Check block in section
            $htmlContent = '';
            if(!empty($block))
            {
                foreach($block as $arrBlock)
                {
                    $arrBlockConfig = self::$_arrReturn['blocks'][$arrBlock['blockName']];

                    //Get all content
                    $htmlContent .= self::getBlockDetail($arrBlockConfig, $arrBlock['blockParams']);
                }
            }
            
            //Set sections with content
            self::setSection($sectionName, $htmlContent);
        }
        
        //var_dump(self::$_layout->_sections); exit;
    }


    /**
     * Get block detail
     * @param type $arrParams 
     */
    public static function getBlockDetail($arrConfig, $arrParams)
    {
        //Try to get data
        try
        {
            //If empty
            if(empty($arrConfig))
            {           
                return '';
            }
            
            //Startup View Render
            $view = new Zend_View();

            //Return content
            $content = $view->action($arrConfig['BlockAction'], $arrConfig['BlockController'], $arrConfig['BlockModule'], $arrParams);

            //Return content
            return $content;
        }
        catch(Zend_Exception $ex)
        {
            // Write log
            echo $ex->getMessage();
        }
        
        //Return default
		return '';
    }
}

